# gozerlib/wave/event.py
#
#

""" google wave events. """

## gozerlib imports
from gozerlib.utils.auth import finduser
from gozerlib.eventbase import EventBase

## basic imports

import logging
import cgi

class WaveEvent(EventBase):


    """ a wave event. """

    def __init__(self):
        EventBase.__init__(self)
        self.type = "wave"
        self.msg = False

    def parse(self, bot, properties, context):

        """ parse properties and context into a WaveEvent. """

        logging.debug("WaveEvent created: %s" % repr(properties))
        self.bot = bot
        self.properties = properties
        self.context = context
        #logging.debug("context: %s" % dir(self.context))
        #logging.debug("serialized: %s" % str(self.context.Serialize()))
        self.blipId = properties['blipId']
        self.blip = context.GetBlipById(self.blipId)
        if not self.blip:
            logging.warn("can't get blip id: %s" % self.blipId)
            self.contributors = []
            self.doc = None
            self.txt = ""
            self.userhost = ""
        else:
            logging.debug("blip: %s" % dir(self.blip))
            self.contributors = self.blip.GetContributors()
            self.doc = self.blip.GetDocument()
            #logging.debug("document: %s" % dir(self.doc))
            self.origtxt = self.doc.GetText()
            self.txt = self.origtxt.strip()
            self.userhost = self.blip.GetCreator()
        self.auth = self.userhost
        #logging.debug("auth is %s" % self.auth)
        self.root = context.GetRootWavelet()
        #logging.debug("root: %s" % dir(self.root))
        self.channel = self.root.waveId
        self.makeargs()        
        return self

    def __deepcopy__(self, a):

        """ deepcopy a wave event. """

        return WaveEvent().copyin(self).parse(self.bot, self.properties, self.context)

    def _raw(self, outtxt, root=None):

        """ send raw text to the server .. creates a blip on the root. """

        if root:
            blip = root.CreateBlip()
        else:
            blip = self.root.CreateBlip()

        blip.GetDocument().AppendText(outtxt)

    def reply(self, txt, resultlist=[], nritems=False, *args, **kwargs):

        """ reply to blip. """

        if self.checkqueues(resultlist):
            return

        if resultlist:
            outtxt = txt + u" " + u' .. '.join(resultlist)
        else:
            outtxt = txt

        if not outtxt:
            return

        #self.doc.SetText(cgi.escape(outtxt))
        (res1, res2) = self.less(outtxt)
        self.write(res1)
        if res2:
            self.write(res2)

    def write(self, outtxt):

        """ write outtxt to the server. """

        logging.debug("wave - output: %s" % outtxt)
        if not self.child:
            self.child = self.blip.CreateChild()
            logging.debug("child: %s" % dir(self.child))

        if self.replied:
            self.child.GetDocument().AppendText("\n\n")

        self.child.GetDocument().AppendText(outtxt)
        self.replied = True

    def write_root(self, outtxt, root=None):

        """ write to the root of a wave. """

        self._raw(outtxt, root)

    def replyroot(self, txt, resultlist=[], nritems=False, root=None, *args, **kwargs):

        """ reply to wave root. """

        if self.checkqueues(resultlist):
            return

        if resultlist:
            outtxt = txt + u" " + u' .. '.join(resultlist)
        else:
            outtxt = txt

        if not outtxt:
            return

        (res1, res2) = self.less(outtxt)
        self.write_root(res1, root)
        if res2:
            self.write_root(res2, root)
